package com.codeoss.framework.webide.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeoss.framework.context.UserContext;
import com.codeoss.framework.webide.response.WebIDEResponse;
import com.codeoss.framework.webide.service.IWebIDEService;
import com.codeoss.module.ir.model.IrApplication;
import com.codeoss.module.ir.model.IrMenu;
import com.codeoss.module.ir.model.IrModel;
import com.codeoss.module.ir.model.IrPage;
import com.codeoss.module.ir.model.IrReport;
import com.codeoss.module.ir.model.IrWorkflow;
import com.codeoss.module.ir.service.IIrApplicationService;
import com.codeoss.module.ir.service.IIrMenuService;
import com.codeoss.module.ir.service.IIrModelService;
import com.codeoss.module.ir.service.IIrPageService;
import com.codeoss.module.ir.service.IIrReportService;
import com.codeoss.module.ir.service.IIrWorkflowService;

@Service
public class WebIDEService implements IWebIDEService{
    @Autowired
    private IIrModelService irModelService;
    @Autowired
    private IIrPageService devPageService;
    @Autowired
    private IIrReportService irReportService;
    @Autowired
    private IIrWorkflowService irWorkflowService;
    @Autowired
    private IIrApplicationService irApplicationService;
    @Autowired
	private IIrMenuService irMenuService;
	@Override
	public WebIDEResponse init() {
		// TODO Auto-generated method stub
		WebIDEResponse webIDEResponse=new WebIDEResponse();
		UserContext userContext=UserContext.getCurrentUserContext();
		webIDEResponse.setUsername(userContext.getUsername());
		return webIDEResponse;
	}
	
	@Override
	public WebIDEResponse getRecentModelList() {
		// TODO Auto-generated method stub
		WebIDEResponse webIDE2Response=new WebIDEResponse();
		QueryWrapper<IrModel> modelQuery=new QueryWrapper<IrModel>();
		modelQuery.orderByDesc("update_date");
		Page<IrModel> page = new Page<IrModel>(0, 10);
		IPage<IrModel> resultPage = irModelService.page(page, modelQuery);
		List<IrModel> list = resultPage.getRecords();
		webIDE2Response.setRecentModelList(list);
		for(int i=0;i<list.size();i++) {
			IrModel devModel=(IrModel)list.get(i);
			devModel.setJsonContent(null);
		}
		return webIDE2Response;
	}

	@Override
	public WebIDEResponse getRecentPageList() {
		// TODO Auto-generated method stub
		WebIDEResponse webIDE2Response=new WebIDEResponse();
		QueryWrapper<IrPage> pageQuery=new QueryWrapper<IrPage>();
		pageQuery.orderByDesc("update_date");
		Page<IrPage> page = new Page<IrPage>(0, 10);
		IPage<IrPage> resultPage = devPageService.page(page, pageQuery);
		List<IrPage> list = resultPage.getRecords();
		webIDE2Response.setRecentPageList(list);
		for(int i=0;i<list.size();i++) {
			IrPage rPage=(IrPage)list.get(i);
			rPage.setComponentModelJson(null);
			rPage.setComponentTreeModelJson(null);
		}
		return webIDE2Response;
	}

	@Override
	public WebIDEResponse getRecentReportList() {
		// TODO Auto-generated method stub
		WebIDEResponse webIDE2Response=new WebIDEResponse();
		QueryWrapper<IrReport> reportQuery=new QueryWrapper<IrReport>();
		reportQuery.orderByDesc("update_date");
		Page<IrReport> page = new Page<IrReport>(0, 10);
		IPage<IrReport> resultPage = irReportService.page(page, reportQuery);
		List<IrReport> list = resultPage.getRecords();
		webIDE2Response.setRecentReportList(list);
		for(int i=0;i<list.size();i++) {
			IrReport irReport=(IrReport)list.get(i);
			irReport.setJsonContent(null);
		}
		return webIDE2Response;
	}

	@Override
	public WebIDEResponse getRecentWorkflowList() {
		// TODO Auto-generated method stub
		WebIDEResponse webIDE2Response=new WebIDEResponse();
		QueryWrapper<IrWorkflow> workflowQuery=new QueryWrapper<IrWorkflow>();
		workflowQuery.orderByDesc("update_date");
		Page<IrWorkflow> page = new Page<IrWorkflow>(0, 10);
		IPage<IrWorkflow> resultPage = irWorkflowService.page(page, workflowQuery);
		List<IrWorkflow> list = resultPage.getRecords();
		webIDE2Response.setRecentWorkflowList(list);
		for(int i=0;i<list.size();i++) {
			IrWorkflow devWorkflow=(IrWorkflow)list.get(i);
			devWorkflow.setJsonContent(null);
		}
		return webIDE2Response;
	}

	@Override
	public WebIDEResponse getRecentApplicationList() {
		// TODO Auto-generated method stub
		WebIDEResponse webIDE2Response=new WebIDEResponse();
		QueryWrapper<IrApplication> applicationQuery=new QueryWrapper<IrApplication>();
		applicationQuery.orderByDesc("update_date");
		Page<IrApplication> page = new Page<IrApplication>(0, 10);
		IPage<IrApplication> resultPage = irApplicationService.page(page, applicationQuery);
		List<IrApplication> list = resultPage.getRecords();
		webIDE2Response.setRecentApplicationList(list);
		return webIDE2Response;
	}

	@Override
	public WebIDEResponse getPageCodeByMenuCode(String menuCode) {
		// TODO Auto-generated method stub
		WebIDEResponse webIDEResponse=new WebIDEResponse();
		QueryWrapper<IrMenu> menuQuery=new QueryWrapper<IrMenu>();
		menuQuery.eq("code", menuCode);
		IrMenu irMenu=irMenuService.getOne(menuQuery);
		webIDEResponse.setMenuType(irMenu.getMenuType());
		webIDEResponse.setPageCode(irMenu.getCode());
		return webIDEResponse;
	}
	
}
